package com.tinysand.advert.utils;

import org.apache.commons.lang.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;

/**
 * Created By TINY
 * Create Time 2020/2/27
 **/
public abstract class CommonUtils {

    public static <K, V> V getOrCreate(
            K key, Map<K, V> map, Supplier<V> creator) {
        return map.computeIfAbsent(key, k -> creator.get());
    }

    public static String stringConcat(String... args) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String arg : args) {
            stringBuilder.append(arg);
            stringBuilder.append("-");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }

    public static Date parseString2Date(String dateString) {
        DateFormat dateFormat = new SimpleDateFormat(
                "EEE MMM dd HH:mm:ss zzz yyy",
                Locale.US
        );
        try {
            return DateUtils.addHours(dateFormat.parse(dateString), -8);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }
}
